package org.dromara.third.convert.safeMeasure.web;

import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.BusinessServiceException;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.ValidateUtil;
import org.dromara.system.domain.SafetyMeasure;
import org.dromara.system.mapper.SafetyMeasureMapper;
import org.dromara.third.auth.beans.constans.ExceptionEnum;
import org.dromara.third.auth.common.auth.TokenThreadLocal;
import org.dromara.third.auth.util.AESUtil;
import org.dromara.third.convert.safeMeasure.dto.SafeMeasureDTO;
import org.dromara.third.convert.safeMeasure.validator.SafeMeasureValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/api/v1")
public class ConvertSafeMeasureController {

    @Autowired
    private SafetyMeasureMapper safetyMeasureMapper;

    @PostMapping("/safetyMeasure")
    @Transactional
    public R<Void> addSafetyMeasure(@RequestBody String jsonReq) {
        String orgCode = TokenThreadLocal.getThreadLocal();
        SafeMeasureDTO safeMeasureDTO;
        try {
            String decrypt = AESUtil.decrypt(jsonReq);
            safeMeasureDTO = JSONObject.parseObject(decrypt, SafeMeasureDTO.class);
        } catch (Exception e) {
            log.info("解析参数异常");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), ExceptionEnum.AuthException06.getMessage());
        }
        // 校验参数
        List<String> errorMessageList = new ArrayList<>();
        ValidateUtil.validate(errorMessageList, safeMeasureDTO, new SafeMeasureValidator());
        if (!CollectionUtils.isEmpty(errorMessageList)) {
            String errorMessage = StringUtils.join(errorMessageList, ",");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), errorMessage);
        }
        SafetyMeasure safetyMeasure = assembleSafeMeasureData(orgCode, safeMeasureDTO);
        int count = safetyMeasureMapper.countWithOutDelFlag(safetyMeasure.getId());
        if (count == 0) {
            safetyMeasureMapper.insert(safetyMeasure);
        } else {
            safetyMeasureMapper.updateWithOutDelFlag(safetyMeasure);
        }
        return R.ok();
    }

    private SafetyMeasure assembleSafeMeasureData(String orgCode, SafeMeasureDTO dto) {
        SafetyMeasure safetyMeasure = new SafetyMeasure();
        try {
            safetyMeasure.setId(dto.getDataId());
            safetyMeasure.setZyid(dto.getZyid());
            safetyMeasure.setZylx(dto.getZylx());
            safetyMeasure.setXh(dto.getXh());
            safetyMeasure.setAqcsnr(dto.getAqcsnr());
            safetyMeasure.setSfsj(dto.getSfsj());
            safetyMeasure.setQrr(dto.getQrr());
            safetyMeasure.setQmtp(dto.getQmtp());
            safetyMeasure.setQrsj(DateUtils.parseDate(dto.getQrsj()));
            safetyMeasure.setDelFlag(Long.parseLong(dto.getDelFlag()));
            safetyMeasure.setCreateDept(Long.parseLong(dto.getCreateDept()));
            safetyMeasure.setCreateBy(Long.parseLong(dto.getCreateBy()));
            safetyMeasure.setCreateTime(DateUtils.parseDate(dto.getCreateTime()));
            safetyMeasure.setUpdateBy(Long.parseLong(dto.getUpdateBy()));
            safetyMeasure.setUpdateTime(DateUtils.parseDate(dto.getUpdateTime()));
            safetyMeasure.setTenantId(orgCode);
        } catch (Exception e) {
            throw new BusinessServiceException(ExceptionEnum.AuthException07.getCode(), "组装安全措施信息异常");
        }
        return safetyMeasure;
    }
}
